package com.kgc.storeprovider.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kgc.basic.util.PageInfo;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.store.pojo.InStore;
import com.kgc.store.pojo.OutStore;
import com.kgc.storeprovider.dao.InStoreDetailMapper;
import com.kgc.storeprovider.dao.InStoreMapper;
import com.kgc.storeprovider.dao.OutStoreDetailMapper;
import com.kgc.storeprovider.dao.OutStoreMapper;
import com.kgc.storeprovider.service.OutStoreService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class OutStoreServiceImpl implements OutStoreService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private OutStoreMapper outStoreMapper;

    @Autowired
    private OutStoreDetailMapper outStoreDetailMapper;

    @Override
    public ResponseMsg getOutStoreList(Map<String, Object> map) {
        logger.info("OutStoreServiceImpl getOutStoreList map:"+map);
        ResponseMsg msg = new ResponseMsg();
        PageInfo<OutStore> pageInfo = new PageInfo<>();
        if(map.containsKey("pageNo")){
            pageInfo.setPageNo(Integer.valueOf(map.get("pageNo").toString()));
        }
        if(map.containsKey("pageSize")){
            pageInfo.setPageSize(Integer.valueOf(map.get("pageSize").toString()));
        }
        Page<Object> page = PageHelper.startPage(pageInfo.getPageNo(), pageInfo.getPageSize());
        List<OutStore> outStoreList = outStoreMapper.getOutStoreList(map);
        logger.debug("OutStoreServiceImpl getOutStoreList outStoreList:"+outStoreList);
        pageInfo.setList(outStoreList);
        pageInfo.setPages(page.getPages());
        pageInfo.setTotal((int)page.getTotal());
        msg.setCode("200");
        msg.setData(pageInfo);
        logger.debug("OutStoreServiceImpl getOutStoreList msg:"+msg);
        return msg;
    }

    @Override
    public ResponseMsg delOutStore(Integer id) {
        ResponseMsg msg = new ResponseMsg();
        OutStore outStore = outStoreMapper.getOutStoreById(id);
        if(outStore==null){
            msg.setData(false);
            msg.setCode("201");
            return msg;
        }
        int delDetailCount = outStoreDetailMapper.delOutStoreDetailByOutStoreCode(outStore.getCode());
        int delCount = outStoreMapper.delOutStore(id);
        if(delCount>0){
            msg.setData(true);
            msg.setCode("200");
        }else{
            msg.setData(false);
            msg.setCode("201");
        }
        return msg;
    }

    @Override
    public ResponseMsg addOutStore(OutStore outStore) {
        ResponseMsg msg = new ResponseMsg();
        int insertCount = outStoreMapper.insertOutStore(outStore);
        if(insertCount>0){
            msg.setData(true);
            msg.setCode("200");
        }else{
            msg.setData(false);
            msg.setCode("201");
        }
        return msg;
    }

    @Override
    public ResponseMsg getOutStoreByCode(String code) {
        ResponseMsg msg = new ResponseMsg();
        OutStore outStore = outStoreMapper.getOutStoreByCode(code);
        return msg;
    }

    @Override
    public ResponseMsg changeStatus(String code, Integer status) {
        ResponseMsg msg = new ResponseMsg();
        int updateCount = outStoreMapper.changeStatus(code,status);
        if(updateCount>0){
            msg.setData(true);
            msg.setCode("200");
        }else{
            msg.setData(false);
            msg.setCode("201");
        }
        return msg;
    }
}
